package com.geek.water.controller;

import com.geek.water.constant.CodeConstant;
import com.geek.water.constant.MessageConstant;
import com.geek.water.result.Result;
import com.geek.water.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.geek.water.entity.Permission;

import java.util.List;
import java.util.Map;

/**
 * @author chenmin
 * @desc 权限管理控制器
 * @date 2025/01/08
 */
@RestController
@RequestMapping("/admin/permission")
@Slf4j
@Api(tags = "后台系统-权限管理接口")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    @ApiOperation("获取权限树")
    @GetMapping("tree")
    public Result getPermissionTree() {
        try {
            // 从数据库动态获取权限树结构
            List<Map<String, Object>> permissionTree = permissionService.getPermissionTree();

            log.info("获取权限树成功，节点数量: {}", permissionTree.size());
            return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_SUCCESS, permissionTree);
        } catch (Exception e) {
            log.error("获取权限树失败", e);
            return new Result(CodeConstant.FAIL, MessageConstant.FAIL + ": " + e.getMessage());
        }
    }

    @ApiOperation("获取所有权限列表")
    @GetMapping("list")
    public Result getPermissionList() {
        try {
            List<?> permissions = permissionService.getAllPermissions();

            log.info("获取权限列表成功，数量: {}", permissions.size());
            return new Result(CodeConstant.SUCCESS, MessageConstant.QUERY_SUCCESS, permissions);
        } catch (Exception e) {
            log.error("获取权限列表失败", e);
            return new Result(CodeConstant.FAIL, MessageConstant.FAIL + ": " + e.getMessage());
        }
    }
}
